function leadTable(){

    $('#studentTb').bootstrapTable({
        url:'/api/student/getbypage',
        striped:true,//设置为true会有隔行变色效果
        pagination:true,//设置为true会在底部显示分页条
        singleSelect: false,//设置为false将禁止多选
        pageSize:3,//如果设置了分页，每页数据条数
        pageNumber:1,//如果设置了分页。首页页码
        sidePagination: "server",//设置在哪里进行分页，可选项为“client”
        queryParams:function (params){
            let paraObj = {
                size:params.limit,
                page:params.offset/params.limit,
                sort:"id",
                direct:"desc",
                name:$("#searchName").val(),
                sex:$("#searchSex").val(),
                classname:$("#searchClassName").val(),
                age:$("#searchAge").val(),
                score:$("#searchScore").val()
            };

            return paraObj;
        },
        columns:[{
            field:'id',
            title:'序号'
        },{
            field: 'no',
            title: '学号'
        },{
            field: 'name',
            title: '姓名'
        },{
            field: 'sex',
            title: '性别'
        },{
            field: 'classname',
            title: '班级'
        },{
            field: 'age',
            title: '年龄'
        },{
            field: 'score',
            title: '成绩'
        },{
            title: '操作',
            width: '100px',
            formatter:function (data, row, meta){
                let operators = '<a href="#" class="btn btn-mint btn-xs btn-icon" onclick=editStudent(' + row.id + ') title="title">编辑</a> ' +
                    '<a href="#" class="btn btn-mint btn-xs btn-icon" onclick=deleteStudent(' + row.id + ') title="title">删除</a>';
                return operators;
            }
        }]
    });
}

function search(){
    $('#studentTb').bootstrapTable("destroy");
    leadTable();
}

//
// function leadTable(){
//
//     let inputname = $('#inputname').val();
//     let a = "";
//     if(inputname != ""){
//         a = "/api/student/list?name="+inputname;
//     }else{
//         a = "/api/student/list"
//     }
//     $.ajax({
//         method:'GET',
//         url:a
//     }).done(function (rs) {
//             console.log(JSON.stringify(rs))
//             let html = "";
//             rs.forEach(function (item) {
//                 html += "<tr>"
//                     + "<td>" + item.id + "</td>"
//                     + "<td>" + item.no + "</td>"
//                     + "<td>" + item.name + "</td>"
//                     + "<td>" + item.sex + "</td>"
//                     + "<td>" + item.classname + "</td>"
//                     + "<td>" + item.age + "</td>"
//                     + "<td>" + item.score + "</td>"
//
//                     + "<td> <a href='#' onclick='editStudent(" + item.id + ")'>编辑</a>   <a href='#' onclick='deleteStudent(" +
//                     item.id + ")'>删除</a></td>"
//
//                     + "</tr>";
//             });
//             $("#studentTb").html(html);
//         }
//     )
// }

function saveStudent(){
    let data = $("#studentform").serialize();

    let id = $("#id").val();
    let requestObj = null;
    if(id < 1){
        requestObj = {
            method: 'post',
            url: '/api/student/insert',
            data:data
        }
    }
    else {
        //更新
        requestObj = {
            method: 'put',
            url: '/api/student/update',
            data:data
        }
    }

    $.ajax(requestObj).done(function (){
        $('#studentModel').modal('hide');
        $('#studentTb').bootstrapTable("destroy");
        leadTable();
    });
}

function editStudent(id){

    // $('#studentModel').modal('show');
    $.ajax({
        method:'GET',
        url:'/api/student/get/'+id
    }).done(function (rs){
        $('#id').val(rs.id);
        $('#no').val(rs.no);
        $('#name').val(rs.name);
        $('#sex').val(rs.sex);
        $('#classname').val(rs.classname);
        $('#age').val(rs.age);
        $('#score').val(rs.score);
        $('#studentModel').modal('show');
    });
}

function preAdd(){
    $("#id").val(0);//提示新增
    $('#studentModel').modal('show');

}

function deleteStudent(id) {
    if (confirm("您是否真的要删除吗？别后悔哦！")) {
        $.ajax({
            method: 'delete',
            url: '/api/student/delete/' + id
        }).done(function () {
            $('#studentTb').bootstrapTable("destroy");
            leadTable();
        });
    }// end if
}

function clearForm(){
    $("#no").val("");
    $("#name").val("");
    $("#sex").val("男");
    $("#classname").val("");
    $("#age").val("");
    $("#score").val("");
}

$(function () {
    $.ajax({
        url:"/api/login/nginxlogin"
    }).done(function (rs){
        if(rs >= 0){
            //成功
            leadTable();
        }else {
            window.location.href="login/user"
        }
    });
    // leadTable();
})

$('#studentModel').on('hidden.bs.modal',clearForm)//清空代码